package com.kaka.starter.swagger.service;

import io.swagger.models.auth.In;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author kaka
 * @Date 2019-11-15
 */
public class SwaggerService {

    private List<Response> getResponseMessageList() {
        List<Response> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseBuilder().code("997").description("未知错误").build());
        responseMessageList.add(new ResponseBuilder().code("998").description("用户不存在").build());
        responseMessageList.add(new ResponseBuilder().code("999").description("业务逻辑异常").build());
        responseMessageList.add(new ResponseBuilder().code("403").description("身份验证失败").build());
        responseMessageList.add(new ResponseBuilder().code("404").description("请求路径异常").build());
        responseMessageList.add(new ResponseBuilder().code("429").description("限流异常").build());
        responseMessageList.add(new ResponseBuilder().code("400").description("参数校验异常").build());
        responseMessageList.add(new ResponseBuilder().code("500").description("服务器内部错误").build());
        responseMessageList.add(new ResponseBuilder().code("501").description("Hystrix异常").build());
        responseMessageList.add(new ResponseBuilder().code("503").description("服务器异常未发现").build());
        return responseMessageList;
    }

    private ApiInfo getApiInfo(String appName) {
        // 自定义信息
        StringVendorExtension extension = new StringVendorExtension("serviceName", appName);
        return new ApiInfoBuilder()
                //页面标题
                .title("标题：" + appName + "使用Swagger3构建API接口文档")
                //描述
                .description("描述：用于" + appName + "接口查看")
                //创建人
                .contact(new Contact("kaka", "", ""))
                //标注服务名称
                .extensions(Collections.singletonList(extension))
                .version("版本号：3.0")
                .build();
    }

    private List<SecurityScheme> getPars(String kakaUserInfo) {
        List<SecurityScheme> list = new ArrayList<>();
        ApiKey apiKey = new ApiKey(kakaUserInfo, kakaUserInfo, In.HEADER.toValue());
        list.add(apiKey);
        return list;
    }

    public Docket getDocket(String appName, String packagePath) {
        List<Response> responseMessageList = getResponseMessageList();

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(getApiInfo(appName))
                .select()
                //设置包路径
                .apis(RequestHandlerSelectors.basePackage(packagePath))
                .paths(PathSelectors.any())

                .build()
                //.securitySchemes(getPars(kakaUserInfo))
                .globalResponses(HttpMethod.GET, responseMessageList)
                .globalResponses(HttpMethod.POST, responseMessageList)
                .globalResponses(HttpMethod.PUT, responseMessageList)
                .globalResponses(HttpMethod.DELETE, responseMessageList)
                ;
    }
}
